package com.zhdl.modules.process;

import cn.hutool.log.Log;
import com.project.common.thread.ThreadScheduledPool;
import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.modules.common.logging.service.OptRecordService;
import com.zhdl.modules.demo.aoptest.OptLog;
import com.zhdl.modules.demo.dglProcess.DGLMainProcess;
import com.zhdl.modules.demo.entity.MachineConf;
import com.zhdl.modules.demo.testprocess.TestMainProcess;
import com.zhdl.network.strategy.common_client.impl.CommonProtocolStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 此类持有项目主流程管理类 外部通过此类控制主流程 开始 暂停 急停等操作
 */
@Service
@Slf4j
public class ProcessStatusExchangeHolder {
//    @Resource
//    TestMainProcess baseProcess;
    //    @Resource
//    DCTMainProcess baseProcess;
    @Resource
    DGLMainProcess baseProcess;
    @Resource
    OptRecordService optRecordService;
    public static MachineConf machineConf;
    @Resource
    CommonProtocolStrategy commonProtocolStrategy;
    @Resource
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    //    @Resource
//    TestTimeOutProcess baseProcess;
    public BaseProcess getBaseProcess() {
        return baseProcess;
    }

    @OptLog(title = "开机初始化")
    public void init() {
        baseProcess.initHardware();
        //  optRecordService.saveRecords("开机初始化");
        machineConf = new MachineConf(this);

    }


    @OptLog(title = "初始化位置")
    public void initLocation() {
        baseProcess.initLocation(s -> {
            baseProcess.statusHolder.initComplete = true;
        });
    }

    @OptLog(title = "程序启动")
    public void start() {
        optRecordService.saveRecords("程序启动");
        baseProcess.statusHolder.start();
    }

    public void stop() {
        baseProcess.statusHolder.stop();
    }

    @OptLog(title = "程序停止")
    public void onPause() {
        baseProcess.statusHolder.stop();
        optRecordService.saveRecords("程序停止");
    }

    @OptLog(title = "程序急停")
    public void release() {
        baseProcess.statusHolder.release();
    }

    public void setAuto(boolean b) {
        baseProcess.statusHolder.setAuto(b);
        optRecordService.saveRecords("手自动切换" + b);
    }

    @OptLog(title = "手动清除所有报警")
    public void cancelAlarm() {
        baseProcess.statusHolder.reset();
    }

    @OptLog(title = "调试模式切换")
    public void switchTest(boolean test) {
        baseProcess.statusHolder.setTestModel(test);
    }


    public void start(int workPlace) {
        if (!baseProcess.statusHolder.isStart){
            log.info("当前整机处于停止状态!");
            return;
        }
        DGLMainProcess.optWorkPlace = workPlace;
        optRecordService.saveRecords(workPlace+"工位启动");
        baseProcess.statusHolder.start();
        DGLMainProcess.optWorkPlace = -1;
    }


    @OptLog(title = "程序停止")
    public void onPause(int workPlace) {
        DGLMainProcess.optWorkPlace = workPlace;
        if (workPlace ==-1){
            baseProcess.statusHolder.stop();
        }else{
            baseProcess.statusHolder.childStop();
        }
        DGLMainProcess.optWorkPlace = -1;
        optRecordService.saveRecords(workPlace+"程序停止");
    }

    @OptLog(title = "程序急停")
    public void release(int workPlace) {
        DGLMainProcess.optWorkPlace = workPlace;
        baseProcess.statusHolder.release();
        DGLMainProcess.optWorkPlace = -1;
    }
    @OptLog(title = "手动清除工位报警")
    public void cancelAlarm(int workPlace) {
        DGLMainProcess.optWorkPlace = workPlace;
        baseProcess.statusHolder.reset();
        DGLMainProcess.optWorkPlace = -1;
    }

    @OptLog(title = "初始化工位位置")
    public void initLocation(int workPlace) {
        DGLMainProcess.optWorkPlace = workPlace;
        baseProcess.initLocation(s -> {
            baseProcess.statusHolder.initComplete = true;
            DGLMainProcess.optWorkPlace = -1;
        });
    }
}
